package libs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *              This class is designed to check existence of seminar entity
 *              in database.
 *
 * @author      Jan Papousek
 * @version     2009-03-31
 */
public class SeminarExistence extends AbstractExistence {

    public SeminarExistence(Connection conn, Seminar seminar) throws SQLException {
        if (conn == null) {
            throw new NullPointerException("Argument 'conn' is null.");
        }
        if (seminar == null) {
            throw new NullPointerException("Argument 'seminar' is null.");
        }
        PreparedStatement preparedSt = null;
        ResultSet resultSet = null;

        // check seminar existence
        try {
            preparedSt = conn.prepareStatement("select count(*) from seminars" +
                    " where subject_code=? and number=?");
            preparedSt.setString(1, seminar.getSubject().getCode());
            preparedSt.setInt(2, seminar.getNumber());
            resultSet = preparedSt.executeQuery();
            resultSet.next();
            if(resultSet.getInt(1) != 0) {
                setExistence(true);
            }
        } finally {
                if( preparedSt != null) {
                    preparedSt.close();
                }
        }
    }


}
